﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Service.Forms
{
    public partial class CustomListForm : SimplelListForm
    {
        public CustomListForm()
        {
            InitializeComponent();
        }

        public override void InitializeBinding()
        {
            if (!ViewModel.ContainsKey("data"))
                return;

            repCustomKind.DataSource = Fis.Win.Utils.AppSession.CustomKindCache();
            gridControl1.DataSource = ViewModel["data"];
            gridView1.BestFitColumns();
        }

        private void CustomListForm_Load(object sender, EventArgs e)
        {

            barButtonItem1.Caption = "新建个人客户";
            barButtonItem5.Caption = "新建企业客户";
            barButtonItem5.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            barButtonItem5.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem5_ItemClick);

            var item1 = Fis.Win.Utils.UIUtils.CreateBarItem<DevExpress.XtraBars.BarButtonItem>(this.barManager1, bar2, "复制", 5, Copy_ItemClick);
            Fis.Win.Utils.UIUtils.CreateBarItem<DevExpress.XtraBars.BarButtonItem>(this.barManager1, bar2, "筛选", 6, Query_ItemClick);
            Fis.Win.Utils.UIUtils.CreateBarItem<DevExpress.XtraBars.BarButtonItem>(this.barManager1, bar2, "审核", 7, Audit_ItemClick);

            Query_ItemClick(null, null);
        }

        void Query_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (var f = new CustomQueryForm())
            {
                if (f.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    Close();
                    return;
                }

                ViewModel["filter"] = f.Filter;
            }

            ViewModel = ViewProvider.ExecuteQuery(ViewModel);
            InitializeBinding();
        }

        void Audit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var data = gridView1.GetFocusedDataRow();

            if (data == null)
                return;

            var param = new System.Collections.Hashtable();
            param["SID"] = data["SID"];
            param["audit"] = 1;
            Agile.Engine.View.IView v = null;
            if (data["FCUSTOMKIND"].ToString() == "0")
            {
                v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.PersonEditForm>("PersonViewProvider", Agile.Engine.View.ViewState.Modify, param);
            }
            else
            {
                v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.GroupEditForm>("PersonViewProvider", Agile.Engine.View.ViewState.Modify, param);

            }
            data.ItemArray = (v.ViewModel["data"] as DataTable).Rows[0].ItemArray;
        }

        void Copy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var data = gridView1.GetFocusedDataRow();

            if (data == null)
                return;

            var param = new System.Collections.Hashtable();
            param["SID"] = data["SID"];
            param["copy"] = 1;
            Agile.Engine.View.IView v = null;
            if (data["FCUSTOMKIND"].ToString() == "0")
            {
                v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.PersonEditForm>("PersonViewProvider", Agile.Engine.View.ViewState.Modify, param);
            }
            else
            {
                v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.GroupEditForm>("PersonViewProvider", Agile.Engine.View.ViewState.Modify, param);

            }
            RefreshProcess();
        }

        void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            var v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.GroupEditForm>("PersonViewProvider");

            if (((DialogResult)v.ViewResult) == System.Windows.Forms.DialogResult.OK)
            {
                RefreshProcess();
            }
        }

        protected override void AddNewProcess()
        {
            var v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.PersonEditForm>("PersonViewProvider");

            if (((DialogResult)v.ViewResult) == System.Windows.Forms.DialogResult.OK)
            {
                RefreshProcess();
            }
        }

        protected override void ModifyProcess()
        {
            var data = gridView1.GetFocusedDataRow();

            if (data == null)
                return;

            var param = new System.Collections.Hashtable();
            param["SID"] = data["SID"];
            Agile.Engine.View.IView v = null;
            if (data["FCUSTOMKIND"].ToString() == "0")
            {
                v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.PersonEditForm>("PersonViewProvider", Agile.Engine.View.ViewState.Modify, param);
            }
            else
            {
                v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.GroupEditForm>("PersonViewProvider", Agile.Engine.View.ViewState.Modify, param);

            }
            data.ItemArray = (v.ViewModel["data"] as DataTable).Rows[0].ItemArray;
        }

        protected override void DeleteProcess()
        {
            var data = gridView1.GetFocusedDataRow();

            if (data == null)
                return;

            var param = new System.Collections.Hashtable();
            param["SID"] = data["SID"];

            if (ViewProvider.ExecuteUpdate(param))
            {
                data.Delete();
                gridControl1.RefreshDataSource();
            }
        }

        protected override void RefreshProcess()
        {
            ViewModel = ViewProvider.ExecuteQuery(ViewModel);
            InitializeBinding();
        }

        protected override void FindProcess(object filter)
        {
            gridView1.ApplyFindFilter(filter.ToString());
        }


    }
}
